package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理接口")
@Slf4j
@CrossOrigin
public class AdminIntegralGradeController {

    @Autowired
    IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation(tags = "获取",value = "获取全部")
    public R listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list);
    }

    @GetMapping("/GetById/{id}")
    @ApiOperation(tags = "获取",value = "根据id获取")
    public R getById(@ApiParam(value = "积分id",example = "1") @PathVariable("id") Long id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return R.ok().data("integralGrade",integralGrade);
    }

    @PutMapping("/updateById")
    @ApiOperation(tags = "更新",value = "根据id更新")
    public R updateById(@RequestBody IntegralGrade integralGrade){
        boolean result = integralGradeService.updateById(integralGrade);
        return result?
                R.ok().message("更新成功").code(ResponseEnum.SUCCESS.getCode()):
                R.error().message("更新失败").code(ResponseEnum.SUCCESS.getCode());
    }

    @PostMapping("/save")
    @ApiOperation(tags = "添加",value = "id为空")
    public R save(@RequestBody IntegralGrade integralGrade){
        boolean save = integralGradeService.save(integralGrade);
        return save?
                R.ok().message("添加成功").code(ResponseEnum.SUCCESS.getCode()):
                R.error().message("添加失败").code(ResponseEnum.SUCCESS.getCode());
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(tags = "删除",value = "根据id删除")
    public R removeById(@ApiParam(value = "积分id") @PathVariable("id") Long id){
        boolean result = integralGradeService.removeById(id);
        return result?
                R.ok().message("删除成功").code(ResponseEnum.SUCCESS.getCode()):
                R.error().message("删除失败").code(ResponseEnum.SUCCESS.getCode());
    }


    @GetMapping("/test")
    @ApiOperation(tags = "获取",value = "测试断言类")
    public R test(@RequestParam String name,@RequestParam Integer age){
        log.info("string:"+name+" "+"age:"+ age);
        Assert.isTrue(age>0,ResponseEnum.ERROR);
//        Assert.isTrue(!Objects.equals(name, "aaa") ||age!=null, ResponseEnum.ERROR);
//        Assert.notNull(name,ResponseEnum.ERROR);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("bvi","adfasdf");
        ArrayList<Object> list = new ArrayList<>();
        list.add(123123);
        return R.ok().data("666",6666).data("map",map).data("list",list);
    }






}

